package pointsinfologic

import (
	"common/comCopier"
	"common/dao/model"
	"context"
	"errors"
	"gorm.io/gorm"

	"OemServer/internal/svc"
	"OemServer/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailsLogic {
	return &DetailsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailsLogic) Details(in *pb.UuId) (*pb.Points, error) {
	// todo: add your logic here and delete this line
	if in.Uuid == "" {
		return nil, errors.New("uuid is null")
	}
	var (
		PointInfo = model.PointInfo{}
		res       = pb.Points{}
	)
	db := l.svcCtx.DB
	table := db.Table(model.TableNamePointInfo)
	err := table.Transaction(func(tx *gorm.DB) error {

		if err := tx.Where("uuid = ?", in.Uuid).First(&PointInfo).Error; err != nil {
			return err
		}

		if err := comCopier.CopyWithOption(&res, PointInfo); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return &res, nil
}
